package com.moehu.back.controller.app;


import com.moehu.back.annotation.AppRestController;
import com.moehu.back.annotation.UserLoginToken;
import com.moehu.back.dao.DynamicDao;
import com.moehu.back.model.*;
import com.moehu.back.model.request.DynamicPushModel;
import com.moehu.back.model.response.LoginResponseModel;
import com.moehu.back.model.response.PagingResponseModel;
import com.moehu.back.model.response.UserInfoResponseModel;
import com.moehu.back.service.impl.DynamicServiceImpl;
import com.moehu.back.service.impl.OpusServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/dynamic")
@AppRestController
@Api(tags = "Dynamic 动态相关的接口")
public class DynamicController {

    @Autowired
    DynamicServiceImpl dynamicService;


    @UserLoginToken
    @ApiOperation(value="【需要Token】发布动态", notes="发布新的普通动态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dynamic_introduce", value = "动态的内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dynamic_imgs", value = "动态的图片数组，例如：['地址1','地址2','地址3']", required = true, dataType = "String[]")})
    @PostMapping("/Push")
    public ResultModel<String> Push(@RequestBody DynamicPushModel dy) {
        return dynamicService.addDynamic(dy);
    }


    @ApiOperation(value="个人空间的 动态", notes="获取个人空间的动态，注意：只获取用户自己发布的动态，前端根据接口中返回的dynamic_type 决定进入不同的页面。dynamic_type 为1传入opus_id进入作品详情页面，如果为0传入dynamic_id进入动态详情页。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求的页数", required = false, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "count", value = "每页作品数，默认15", required = false, dataType = "Number", defaultValue="15"),
            @ApiImplicitParam(name = "userid", value = "用户的user_id", required = true, dataType = "Number", defaultValue=""),
    })
    @PostMapping("/PersonalDynamic")
    public ResultModel<PagingResponseModel<List<DynamicModel>>> PersonalDynamic(@RequestBody PagingModel pagingModel) {
        if (pagingModel.getCount()==null) {
            pagingModel.setCount(15);
        }

        if (pagingModel.getPage()==null) {
            pagingModel.setPage(1);
        }
        return dynamicService.personalDynamic(pagingModel);
    }


    @UserLoginToken
    @ApiOperation(value="底部Tabs动态", notes="获取底部Tabs导航中的动态列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求的页数", required = false, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "count", value = "每页作品数，默认15", required = false, dataType = "Number", defaultValue="15"),
    })
    @PostMapping("/TabsDynamic")
    public ResultModel<PagingResponseModel<List<DynamicModel>>> TabsDynamic(@RequestBody PagingModel pagingModel) {
        if (pagingModel.getCount()==null) {
            pagingModel.setCount(15);
        }

        if (pagingModel.getPage()==null) {
            pagingModel.setPage(1);
        }
        return dynamicService.TabsDynamic(pagingModel);
    }


    @ApiOperation(value="普通动态的详情", notes="进入普通动态的详情，投稿动态需要进入作品详情页，这个接口不负责！")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dynamic_id", value = "动态的id", required = true, dataType = "Number")
    })
    @PostMapping("/Info")
    public ResultModel<PagingResponseModel<List<DynamicModel>>> DynamicInfo(@RequestBody Map<String, Integer> dynamic) {
        return dynamicService.dynamicInfo(dynamic.get("dynamic_id"));
    }



    @UserLoginToken
    @ApiOperation(value="【需要Token】动态中，记录用户访问记录", notes="用户进入普通动态详情，和投稿动态详情的时候记录用户访问记录。注意：1：如果用户未登录查看动态详情，那么不调用接口。2：投稿动态也属于动态，但是投稿动态的详情页是作品详情页面，所以需要在作品详情里面判断，是否从动态中进入，如果是调用接口，如果不是不调用接口！！")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user_to_id", value = "被查看的用户的id", required = true, dataType = "Number"),
    })
    @PostMapping("/RecordHistory")
    public ResultModel<String> RecordHistory(@RequestBody Map<String, Integer> dynamic) {
        return dynamicService.recordHistory(dynamic.get("user_to_id"));
    }


    @UserLoginToken
    @ApiOperation(value="【需要Token】获取【最常访问】用户列表", notes="底部Tabs的动态中获取【最常访问】，前端页面只需要显示5条数据，所以 count 可以传5参数！点击查看更多后 count 可以传 15")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求的页数", required = false, dataType = "Number", defaultValue="1"),
            @ApiImplicitParam(name = "count", value = "每页作品数，默认15", required = false, dataType = "Number", defaultValue="15"),
    })
    @PostMapping("/DynamicHistory")
    public ResultModel<PagingResponseModel<List<UserInfoResponseModel>>> GetDynamicHistory(@RequestBody PagingModel pagingModel) {
        if (pagingModel.getCount()==null) {
            pagingModel.setCount(15);
        }

        if (pagingModel.getPage()==null) {
            pagingModel.setPage(1);
        }

        return dynamicService.getDynamicHistory(pagingModel.getPage(), pagingModel.getCount());
    }
}
